/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cratos.xxgramoss.monitor;

import com.cratos.xxgram.platf.base.BaseService;
import com.cratos.xxgram.platf.group.GroupDetail;
import com.cratos.xxgram.platf.message.MessageRecordHis;
import com.cratos.xxgram.platf.user.UserDetail;
import com.cratos.xxgram.platf.user.UserLoginRecord;
import javax.annotation.Resource;
import org.redkale.net.http.RestService;
import org.redkale.source.*;
import static com.cratos.xxgramoss.base.Services.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.redkale.net.http.*;
import org.redkale.service.RetResult;
import org.redkale.source.Range.LongRange;
import org.redkale.util.Utility;

/**
 *
 * @author alter
 */
@RestService(comment = "概况", moduleid = MODULE_MONITOR)
public class MonitorOssService extends BaseService {

    @Resource(name = "platf")
    protected DataSource platfSource;

    @RestMapping(auth = true, actionid = ACTION_QUERY, comment = "概况信息")
    public RetResult queryDataList() {
        Map map = new HashMap<>(); 
        LongRange range = LongRange.todayRange();
        //今日注册用户
        int totalreg = platfSource.getNumberResult(UserDetail.class, FilterFunc.COUNT, 0, "userid", FilterNode.create("createtime", FilterExpress.BETWEEN, range)).intValue();
        //用户总数
        int totaluser = platfSource.getNumberResult(UserDetail.class, FilterFunc.COUNT, 0, "userid", (FilterNode) null).intValue();
        //群总数
        int totalgroup = platfSource.getNumberResult(GroupDetail.class, FilterFunc.COUNT, 0, "groupid", (FilterNode) null).intValue();
        //单聊总数
        FilterNode msgFilter = FilterNode.create("msgkind", FilterExpress.EQUAL, MessageRecordHis.MSG_KIND_PRIVATE).and("createtime", FilterExpress.LESSTHAN, System.currentTimeMillis());
        int totalmsg = platfSource.getNumberResult(MessageRecordHis.class, FilterFunc.COUNT, 0, "messageid", msgFilter).intValue();
        map.put("totalreg",totalreg);
        map.put("totaluser",totaluser);
        map.put("totalgroup",totalgroup);
        map.put("totalmsg",totalmsg);
        return RetResult.success(map);
    }
    
    @RestMapping(auth = true, actionid = ACTION_QUERY, comment = "用户统计")
    public RetResult userOnline(int date) {
        Map resultMap = new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        
        List regList = new ArrayList<>();//用户注册统计
        List msgList = new ArrayList<>();//单聊统计
        List groupList = new ArrayList<>();//建群统计
        List userList = new ArrayList<>();//活跃用户统计
        for(int i= 0;i<date;i++){
            long min = Utility.midnight() - 24 * 60 * 60 * 1000L * i;
            Range r =  new Range.LongRange(min, min + 24 * 60 * 60 * 1000L - 1);
            List list1 = new ArrayList<>();
            int totalreg = platfSource.getNumberResult(UserDetail.class, FilterFunc.COUNT, 0, "userid", FilterNode.create("createtime", FilterExpress.BETWEEN, r)).intValue();
            list1.add(sdf.format(new Date(min)));
            list1.add(totalreg);
            regList.add(list1);
            
            List list2 = new ArrayList<>();
            FilterNode msgFilter = FilterNode.create("msgkind", FilterExpress.EQUAL, MessageRecordHis.MSG_KIND_PRIVATE).and("createtime", FilterExpress.BETWEEN, r);
            int totalmsg = platfSource.getNumberResult(MessageRecordHis.class, FilterFunc.COUNT, 0, "messageid", msgFilter).intValue();
            list2.add(sdf.format(new Date(min)));
            list2.add(totalmsg);
            msgList.add(list2);
            
            List list3 = new ArrayList<>();
            int totalgroup = platfSource.getNumberResult(GroupDetail.class, FilterFunc.COUNT, 0, "groupid", FilterNode.create("createtime", FilterExpress.BETWEEN, r)).intValue();
            list3.add(sdf.format(new Date(min)));
            list3.add(totalgroup);
            groupList.add(list3);
            
            List list4 = new ArrayList<>();
            int totaluser = platfSource.getNumberResult(UserLoginRecord.class, FilterFunc.DISTINCTCOUNT, 0, "userid", FilterNode.create("createtime", FilterExpress.BETWEEN, r)).intValue();
            list4.add(sdf.format(new Date(min)));
            list4.add(totaluser);
            userList.add(list4);
        }
        resultMap.put("regList", regList);
        resultMap.put("msgList", msgList);
        resultMap.put("groupList", groupList);
        resultMap.put("userList", userList);
        return RetResult.success(resultMap);
    }
    
}
